

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% INIT

% parameters
NCOMP = 1; % number of mixture components
USE_WEEKEND = 0; % including weekend tag in models

% path
oldPath = path;
addpath(genpath('../../data/ours/'));

% meta-feature labels
fid = fopen('mflabels.txt');
C = textscan(fid, '%d %s');
fclose(fid);
metafeatureIDs = C{1};
metafeatureLABELs = C{2};
clear C;
numMetafeatures = length(metafeatureIDs);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  TRAIN  %
%                                                                         %
%  A meta-feature classifier is built by computing a MoG for each sensor  %
%  and grouping these models by meta-feature. These clusters represent    %
%  our model for each meta-feature.                                       %
%                                                                         %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

load firings_AB_sec.txt;
if ~USE_WEEKEND,
  firings_AB_sec = firings_AB_sec(:,1:3);
end
load mfmap_AB.txt;
sensorIDs_AB = mfmap_AB(:,1);
numSensors_AB = length(sensorIDs_AB);

allmog_AB = cell(numSensors_AB,1);
mf_clusters_AB = cell(numMetafeatures,1);

fprintf('Computing mixture models for sensors of house AB.\n');

for idx=1:numSensors_AB,
  X = firings_AB_sec(firings_AB_sec(:,1)==sensorIDs_AB(idx),2:end);
  allmog_AB{idx} = gm_process_sensor(X, 'Blue', 0, NCOMP);
end

fprintf('Computing meta-feature clusters for house SB.\n');

for idx=1:numMetafeatures,
  mog_ids = mfmap_AB(:,2)==metafeatureIDs(idx);
  fprintf('Meta-feature %2d (%s): %d sensors.\n', ...
      metafeatureIDs(idx),metafeatureLABELs{idx},sum(mog_ids));
  if sum(mog_ids) == 0,
    % no sensors corresponding to this meta-feature
    mf_clusters_AB{idx} = cell(0);
  else
    mf_clusters_AB{idx} = allmog_AB(mog_ids);
  end
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  TEST  %
%                                                                         %
%  Find for each 'anonymous' cluster (meta-feature of house C) the        %
%  best 'candidate' match (meta-feature of house B).                      %
%                                                                         %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

load firings_C_sec.txt;
if ~USE_WEEKEND,
  firings_C_sec = firings_C_sec(:,1:3);
end
load mfmap_C.txt;
sensorIDs_C = mfmap_C(:,1);
numSensors_C = length(sensorIDs_C);

fprintf('\nComputing mixture models for sensors of house C.\n');

allmog_C = cell(numSensors_C,1);
for idx=1:numSensors_C,
  X = firings_C_sec(firings_C_sec(:,1)==sensorIDs_C(idx),2:end);
  allmog_C{idx} = gm_process_sensor(X, 'Blue', 0, NCOMP);
end

fprintf('Computing meta-feature clusters for house C.\n');

mf_clusters_C = cell(numMetafeatures,1);
for idx=1:numMetafeatures,
  mog_ids = mfmap_C(:,2)==metafeatureIDs(idx);
  fprintf('Meta-feature %2d (%s): %d sensors.\n', ...
      metafeatureIDs(idx),metafeatureLABELs{idx},sum(mog_ids));
  if sum(mog_ids) == 0,
    % no sensors corresponding to this meta-feature
    mf_clusters_C{idx} = cell(0);
  else
    mf_clusters_C{idx} = allmog_C(mog_ids);
  end
end

fprintf('\nFinding best matches for houseC->houseAB (normal).\n');

% for each 'anonymous' cluster in houseC
for idxAnonymous=1:length(mf_clusters_C),
    
  anonymous = mf_clusters_C{idxAnonymous};
  
  minDiv = inf;
  ordCandidate = inf;

  % find best matching 'candidate' cluster from houseB
  for idxCandidate=1:numMetafeatures,
    candidate = mf_clusters_AB{idxCandidate};
    curDiv = compute_cluster_divergence(anonymous, candidate);
    if curDiv < minDiv,
      minDiv = curDiv;
      ordCandidate = idxCandidate;
    end
  end
  
  if isinf(ordCandidate),
    continue;
  else
    fprintf('%s/C -> %s/B (%f)\n', ...
      metafeatureLABELs{idxAnonymous}, ...
      metafeatureLABELs{ordCandidate}, ...
      minDiv);
  end
  
end

% restore old MATLAB path
path(oldPath);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  RESULTS  %
%                                                                         %
%   Computing mixture models for sensors of house AB.
%   Computing meta-feature clusters for house AB.
%   Meta-feature  1 (KitchHeat): 3 sensors.
%   Meta-feature  2 (Toilet): 2 sensors.
%   Meta-feature  3 (BathroomDoor): 1 sensors.
%   Meta-feature  4 (Kitchen): 8 sensors.
%   Meta-feature  5 (KitchStor): 2 sensors.
%   Meta-feature  6 (Outside): 1 sensors.
%   Meta-feature  7 (SleepDoor): 1 sensors.
%   Meta-feature  8 (Sleep): 4 sensors.
%   Meta-feature  9 (Bathroom): 2 sensors.
%   Meta-feature 10 (Other): 0 sensors.
% 
%   Computing mixture models for sensors of house C.
%   Computing meta-feature clusters for house C.
%   Meta-feature  1 (KitchHeat): 1 sensors.
%   Meta-feature  2 (Toilet): 3 sensors.
%   Meta-feature  3 (BathroomDoor): 1 sensors.
%   Meta-feature  4 (Kitchen): 6 sensors.
%   Meta-feature  5 (KitchStor): 2 sensors.
%   Meta-feature  6 (Outside): 1 sensors.
%   Meta-feature  7 (SleepDoor): 1 sensors.
%   Meta-feature  8 (Sleep): 3 sensors.
%   Meta-feature  9 (Bathroom): 2 sensors.
%   Meta-feature 10 (Other): 1 sensors.
% 
%   Finding best matches for houseC->houseAB (normal).
%   KitchHeat/C -> KitchStor/B (0.622149)
%   Toilet/C -> BathroomDoor/B (0.884237)
%   BathroomDoor/C -> KitchHeat/B (1.511404)
%   Kitchen/C -> SleepDoor/B (0.515029)
%   KitchStor/C -> BathroomDoor/B (0.566440)
%   Outside/C -> Toilet/B (0.268251)
%   SleepDoor/C -> Toilet/B (0.337044)
%   Sleep/C -> Sleep/B (0.999778)
%   Bathroom/C -> BathroomDoor/B (0.152273)
%   Other/C -> Kitchen/B (0.135890)
%                                                                         %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
